// app.js
App({
    /*定时器变量*/
    notread_timer: false,

    
    onPageUnload: function() {
        this.globalData.isTabbarPage = false;
    },
    onLaunch() {
        var that = this;
        that.notread_timer = setInterval(function () {
            var pages = getCurrentPages();
            var prevent_page_rouge = pages[pages.length - 1].route;
            //只有在可以看到红点的页面和不冲突页面，才会向服务器 发送请求获取数据，减少服务器负担
            if (prevent_page_rouge == "pages/message/message" || prevent_page_rouge == "pages/group/group" || prevent_page_rouge == "pages/userinfo/my") {
                that.scanInformation();
            }
        }, 10000)
    },
    onPageShow: function () {
        const pageList = getCurrentPages()
        const currentPage = pageList[pageList.length - 1]
    
        // 判断当前页面是否为 tabbar 页面
        if (currentPage.route !== 'pages/function/function' && currentPage.route !== 'pages/myinfo/myinfo') {
          // 将自定义的 tabbar 模板插入到页面中
          currentPage.setData({
            isTabbarPage: false
          })
        } else {
          currentPage.setData({
            isTabbarPage: true
          })
        }
      },
    
    globalData: {
        userInfo: null,
        userID: null,
        version: "1.0",
        Name: "小程序名",
        domain:"http://127.0.0.1:8999/api",
        isTabbarPage: false,
    },

    alert: function (params) {
        var title = params.hasOwnProperty('title') ? params['title'] : '温馨提示 ';
        var content = params.hasOwnProperty('content') ? params['content'] : '';
        var showCancel = params.hasOwnProperty('showCancel') ? params['showCancel'] : false;
        wx.showModal({
            title: title,
            content: content,
            showCancel: showCancel,
            success: function (res) {
                if (res.confirm) {//用户点击确定
                    if (params.hasOwnProperty('cb_confirm') && typeof (params.cb_confirm) == "function") {
                        params.cb_confirm();
                    }
                } else {
                    if (params.hasOwnProperty('cb_cancel') && typeof (params.cb_cancel) == "function") {
                        params.cb_cancel();
                    }
                }
            }
        })
    },

    buildUrl: function (path, params) {
        var url = this.globalData.domain + path;
        var _paramUrl = "";
        if (params) {
            _paramUrl = Object.keys(params).map(function (k) {
                return [encodeURIComponent(k), encodeURIComponent(params[k])].join("=");
            }).join("&");
            _paramUrl = "?" + _paramUrl;
        }
        return url + _paramUrl;
    },

    //构造请求头
    getRequestHeader: function () {
        return {
            'content-type': 'application/x-www-form-urlencoded',
            'Authorization': this.getCache("token"),
        }
    },
})
